package com.mall.app.web;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.app.bean.TbCategory;
import com.mall.app.service.TbCategoryService;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 类别 前端控制器
 * </p>
 *
 * @author LeeSin
 * @since 2020-07-10
 */
@RestController
@RequestMapping("/category")
public class TbCategoryController {
	@Autowired
	private TbCategoryService categoryService;
	/**
	 * 后端类别分页方法
	 * @return
	 */
	
	@RequestMapping("/listPage")
	public Map<String,Object> listPage(int page,int limit,String cname){
		//设置查询条件
		QueryWrapper<TbCategory> queryWrapper = new QueryWrapper<TbCategory>();
		if(cname!=null){
			queryWrapper.like(true, "cname", cname);
		}
		
		
		Map<String,Object> result=new HashMap<String,Object>();
		result.put("code", 0);
		result.put("msg", "");
		result.put("count", categoryService.count(queryWrapper));
		result.put("data", categoryService.page(new Page<TbCategory>(page,limit), queryWrapper).getRecords());
		return result;
	}
	
	@RequestMapping("/list")
	public List<TbCategory> list(){		
		return categoryService.list(null);
	}
	
	@RequestMapping("/delete")
	public boolean removeCategory(@RequestParam("cids[]")List<Integer> cids){
		return categoryService.removeByIds(cids);
	}

	@RequestMapping("/update")
	public boolean updateCategory(TbCategory category) {
		LocalDateTime now = LocalDateTime.now();
		category.setModifyTime(now);
		return categoryService.updateById(category);
	}
	
	@RequestMapping("/insert")
	public boolean insertCategory(TbCategory category) {
		LocalDateTime now = LocalDateTime.now();
		category.setModifyTime(now);
		category.setCreateTime(now);
		return categoryService.save(category);
	}
}

